package com.ygqh.baby.service.impl;

import com.foxinmy.weixin4j.util.StringUtil;
import com.ygqh.baby.ao.AppType;
import com.ygqh.baby.ao.DataStatus;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.mapper.YgUserAuthsMapper;
import com.ygqh.baby.po.YgUserAuths;
import com.ygqh.baby.po.YgUserAuthsExample;
import com.ygqh.baby.po.YgUserAuthsExample.Criteria;
import com.ygqh.baby.service.YgUserAuthsService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Arrays;
import java.util.List;

/**
 * @author admin
 */
@Service
public class YgUserAuthsServiceImpl implements YgUserAuthsService {

    @Autowired
    private YgUserAuthsMapper ygUserAuthsMapper;

    @Override
    public List<YgUserAuths> find() {
        YgUserAuthsExample example = new YgUserAuthsExample();
        example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
        return ygUserAuthsMapper.selectByExample(example);
    }

    @Override
    public int findCount(String unionId, Long platformId) {
        YgUserAuthsExample example = new YgUserAuthsExample();
        Criteria criteria = example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
        if (StringUtil.isNotBlank(unionId)) {
            criteria.andUnionIdEqualTo(unionId);
        }
        if (platformId != null) {
            criteria.andPlatformIdEqualTo(platformId);
        }
        return ygUserAuthsMapper.countByExample(example);
    }

    @Override
    public YgUserAuths findById(Long id) {
        return ygUserAuthsMapper.selectByPrimaryKey(id);
    }

    @Override
    public int save(YgUserAuths ygUserAuths) {
        return ygUserAuthsMapper.insertSelective(ygUserAuths);
    }

    @Override
    public int update(YgUserAuths ygUserAuths) {
        return ygUserAuthsMapper.updateByPrimaryKeySelective(ygUserAuths);
    }

    @Override
    public ResultSet<YgUserAuths> search(QueryInfo queryInfo, String q) {
        int count = ygUserAuthsMapper.countSuper(q);
        if (count == 0) {
            return new ResultSet<>();
        }
        List<YgUserAuths> list = ygUserAuthsMapper.selectSuper(queryInfo, q);
        return new ResultSet<>(count, list);
    }

    @Override
    public int deleteSoft(Long[] ids) {
        Assert.notEmpty(ids);
        YgUserAuthsExample example = new YgUserAuthsExample();
        example.createCriteria().andIdIn(Arrays.asList(ids));
        YgUserAuths record = new YgUserAuths();
        record.setStatus(DataStatus.Delete);

        return ygUserAuthsMapper.updateByExampleSelective(record, example);
    }

    @Override
    public int deleteSoft(Long id) {
        return this.deleteSoft(new Long[]{id});
    }

    @Override
    public int saveOrUpdate(YgUserAuths ygUserAuths) {
        int r;
        if (ygUserAuths.getId() != null) {
            r = this.update(ygUserAuths);
        } else {
            r = this.save(ygUserAuths);
        }
        return r;
    }

    @Override
    public String getOpenId(Long userId, String paltformNo, AppType appType) {
        return ygUserAuthsMapper.getOpenId(userId, paltformNo, appType);

    }

    @Override
    public YgUserAuths findByUnionIdAndPlatform(String unionId, Long platformId) {
        Assert.notNull(unionId, "param unionId can not be null");
        Assert.notNull(platformId, "param platformId can not be null");
        YgUserAuthsExample example = new YgUserAuthsExample();
        example.createCriteria()
                .andUnionIdEqualTo(unionId)
                .andPlatformIdEqualTo(platformId)
                .andStatusEqualTo(DataStatus.Valid.name());
        List<YgUserAuths> list = ygUserAuthsMapper.selectByExample(example);
        return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }

    @Override
    public YgUserAuths findByUserId(Long userId, Long platformId) {
        Assert.notNull(userId, "param userId can not be null");
        Assert.notNull(platformId, "param platformId can not be null");
        YgUserAuthsExample example = new YgUserAuthsExample();
        example.createCriteria()
                .andUserIdEqualTo(userId)
                .andPlatformIdEqualTo(platformId)
                .andStatusEqualTo(DataStatus.Valid.name());
        List<YgUserAuths> list = ygUserAuthsMapper.selectByExample(example);
        return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }

}
